package com.city.parking;

import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.amap.api.services.core.PoiItem;
import com.baidu.mapapi.search.core.PoiInfo;
import com.city.parking.provider.ParkingRecord;
import com.city.parking.util.Utils;

import android.os.Parcel;
import android.os.Parcelable;

public class ParkingItem implements Parcelable {
	public int id;
	public String name;
	public int portCount;
	public int channelCount;
	public int portLeftCount;
	public double charge;
	public double charge1;
	public double charge2;
	public int status;
	public int isFree;
	public int isDeleted;
	public int floor;
	public int type;
	public double longitude = -1;
	public double latitude = -1;
	public String alias = "";
	public String addr;
	public Date date;
	public String mapAddr = "";
	
	private com.baidu.mapapi.model.LatLng lb;
	private com.amap.api.maps.model.LatLng lg;

	private static final String ID = "id";
	private static final String NAME = "name";
	private static final String PORT_CNT = "portCount";
	private static final String CHNL_CNT = "channelCount";
	private static final String PORT_LFT = "portLeftCount";
	private static final String CHARGE = "charge";
	private static final String CHARGE1 = "charge1";
	private static final String CHARGE2 = "charge2";
	private static final String STATUS = "status";
	private static final String ISFREE = "isFree";
	private static final String ISDELETED = "isDeleted";
	private static final String FLOOR = "floor";
	private static final String TYPE = "type";
	private static final String LONGITUDE = "longitude";
	private static final String LATITUDE = "latitude";
	private static final String ALIAS = "alias";
	private static final String POS = "position";
	private static final String MAPADDR = "mapAddr";

	public ParkingItem(Parcel in) {
		id = in.readInt();
		name = in.readString();
		portCount = in.readInt();
		channelCount = in.readInt();
		portLeftCount = in.readInt();
		charge = in.readDouble();
		charge1 = in.readDouble();
		charge2 = in.readDouble();
		status = in.readInt();
		isFree = in.readInt();
		isDeleted = in.readInt();
		floor = in.readInt();
		type = in.readInt();
		longitude = in.readDouble();
		latitude = in.readDouble();
		alias = in.readString();
		addr = in.readString();
		mapAddr = in.readString();
	}
	
	public ParkingItem(){}
	
	public ParkingItem(ParkingRecord pr) {
		id = pr.id;
		name = pr.name;
		addr = pr.addr;
		longitude = pr.longitude;
		latitude = pr.latitude;
	}
	
	public ParkingItem(PoiInfo pi) {
		id = 0;
		name = pi.name;
		addr = pi.address;
		longitude = pi.location.longitude;
		latitude = pi.location.latitude;
		isFree = -1;
	}
	
	public ParkingItem(PoiItem pi) {
		id = 0;
		name = pi.getTitle();
		addr = pi.getAdName();
		longitude = pi.getLatLonPoint().getLongitude();
		latitude = pi.getLatLonPoint().getLatitude();
		isFree = -1;
	}
	
	public com.baidu.mapapi.model.LatLng positionBD() {
		if (lb == null)
			lb =new com.baidu.mapapi.model.LatLng(latitude, longitude);
		return lb;
	}
	
	public com.amap.api.maps.model.LatLng positionGD() {
		if (lg == null)
			lg = new com.amap.api.maps.model.LatLng(latitude, longitude);
		return lg;
	}

	public static ParkingItem fromJson(String str) {
		try {
			JSONObject json = new JSONObject(str);
			ParkingItem ret = new ParkingItem();
			if (json.has(ID))
				ret.id = json.getInt(ID);
			if (json.has(NAME))
				ret.name = json.getString(NAME);
			if (json.has(PORT_CNT))
				ret.portCount = json.getInt(PORT_CNT);
			if (json.has(CHNL_CNT))
				ret.channelCount = json.getInt(CHNL_CNT);
			if (json.has(PORT_LFT))
				ret.portLeftCount = json.getInt(PORT_LFT);
			if (json.has(CHARGE))
				ret.charge = json.getInt(CHARGE);
			if (json.has(CHARGE1))
				ret.charge = json.getInt(CHARGE1);
			if (json.has(CHARGE2))
				ret.charge = json.getInt(CHARGE2);
			if (json.has(STATUS))
				ret.status = json.getInt(STATUS);
			if (json.has(ISFREE))
				ret.isFree = json.getInt(ISFREE);
			if (json.has(ISDELETED))
				ret.isDeleted = json.getInt(ISDELETED);
			if (json.has(FLOOR))
				ret.floor = json.getInt(FLOOR);
			if (json.has(TYPE))
				ret.type = json.getInt(TYPE);
			if (json.has(LATITUDE))
				ret.latitude = json.getDouble(LATITUDE);
			if (json.has(LONGITUDE))
				ret.longitude = json.getDouble(LONGITUDE);
			if (json.has(ALIAS))
				ret.alias = json.getString(ALIAS);
			if (json.has(POS))
				ret.addr = json.getString(POS);
			if (json.has(MAPADDR))
				ret.mapAddr = json.getString(MAPADDR);
			return ret;
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return null;
	}
	
	public static ArrayList<ParkingItem> fromJsonArray(String str) {
		try {
			JSONArray ja = new JSONArray(str);
			ArrayList<ParkingItem> list = new ArrayList<ParkingItem>();
			for (int i=0; i<ja.length(); i++) {
				JSONObject o = ja.getJSONObject(i);
				ParkingItem item = fromJson(o.toString());
				if (item != null)
					list.add(item);
			}
			return list;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public String toJsonStr() {
		JSONObject json = new JSONObject();
		try {
			json.put(ID, id);
			json.put(LATITUDE, latitude);
			json.put(LONGITUDE, longitude);
			return json.toString();
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		return null;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel out, int arg1) {
		// TODO Auto-generated method stub
		out.writeInt(id);
		out.writeString(name);
		out.writeInt(portCount);
		out.writeInt(channelCount);
		out.writeInt(portLeftCount);
		out.writeDouble(charge);
		out.writeDouble(charge1);
		out.writeDouble(charge2);
		out.writeInt(status);
		out.writeInt(isFree);
		out.writeInt(isDeleted);
		out.writeInt(floor);
		out.writeInt(type);
		out.writeDouble(longitude);
		out.writeDouble(latitude);
		out.writeString(alias);
		out.writeString(addr);
		out.writeString(mapAddr);
	}

	public static final Parcelable.Creator<ParkingItem> CREATOR = new Creator<ParkingItem>() {
        @Override
        public ParkingItem[] newArray(int size) {
            return new ParkingItem[size];
        }

        @Override
        public ParkingItem createFromParcel(Parcel source) {
            return new ParkingItem(source);
        }
    };
}
